package com.egao.api.controller;


import com.egao.common.core.Constants;
import com.egao.common.core.security.JwtUtil;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.UserService;
import com.egao.orderB.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * http://192.168.1.110/swagger-ui.html
 */
@RequestMapping("/api/nt/customerLogin")
@RestController
@Api(value = "货代下单接口Api",tags = {"货代下单接口Api"})
public class ApiCustomerLoginController extends BaseController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;

    /**
     * web注册用户
     */
    @ApiOperation(value = "客户登录",notes = "" +
            "username 帐号 必填\n" +
            "password 密码 必填\n" +
            "",httpMethod = "POST")
    @PostMapping("/login")
    public JsonResult login(User user) {
        try{
            User user2 = userService.getUserByUsername(new User(user.getUsername()));
//            String pwd = userService.encodePsw(user.getPassword());
            if (user2.getPasswordorigin().equals(user.getPassword())) {
                String access_token = JwtUtil.buildToken(user.getUsername(), Constants.TOKEN_EXPIRE_TIME, Constants.TOKEN_KEY);
                return JsonResult.ok("登录成功!").put("access_token", access_token);
            } else {
                return JsonResult.error("登录失败!");
            }
        } catch (Exception e) {
            return JsonResult.error("登录失败");
        }
    }


}
